package com.mes.defece.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.defece.mapper.MaterialProductsMapper;
import com.mes.defece.service.MaterialProductsService;
import com.mes.pojo.defece.MaterialProducts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 侯伟豪
 * @Company 起点编程
 * @Description
 * @Time 2024/10/21 10:13
 */
@Service
public class MaterialProductsServiceImpl implements MaterialProductsService {
    @Autowired
    private MaterialProductsMapper materialProductsMapper;

    @Override
    public MESResult selectAll(MaterialProducts materialProducts, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<MaterialProducts> materialProductsList = materialProductsMapper.selectAll(materialProducts);
        return MESResult.success(new PageInfo<>(materialProductsList));
    }

    @Override
    public MESResult addMaterialProducts(MaterialProducts materialProducts) {
        if (materialProducts.getMaterialProductStatus() == null){
            materialProducts.setMaterialProductStatus(1);
        }
        if (materialProducts.getMaterialProductTemplateId() == null){
            materialProducts.setMaterialProductTemplateId(1);
        }
        return MESResult.success(materialProductsMapper.insert(materialProducts));
    }

    @Override
    public MESResult selectById(Long materialProductId) {
        return MESResult.success(materialProductsMapper.selectById(materialProductId));
    }

    @Override
    public MESResult updateMaterialProducts(MaterialProducts materialProducts) {
        return MESResult.success(materialProductsMapper.updateDefeceById(materialProducts));
    }

    @Override
    public MESResult deleteMaterialProducts(Long materialProductId, Integer materialProductStatus) {
        MaterialProducts materialProducts = new MaterialProducts();
        materialProducts.setMaterialProductId(materialProductId);
        materialProducts.setMaterialProductStatus(99);
        return MESResult.success(materialProductsMapper.deleteMaterialProductById(materialProducts));
    }
}